package com.example.demo.controller;

import com.example.demo.datasource.DBContextHolder;
import com.example.demo.entity.AccountBook;
import com.example.demo.service.IAccountBookService;
import com.example.demo.service.ISysConfigService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class SysConfigController {
    @Resource
    private ISysConfigService iSysConfigService;
    @Resource
    private IAccountBookService iAccountBookService;

    @GetMapping("list")
    public ResponseEntity<?> list(HttpServletRequest request){

        Map<String, Object> map = new HashMap<String, Object>();
        map.put(DBContextHolder.DATASOURCE_KEY, "test2");
        map.put(DBContextHolder.DATASOURCE_DRIVER, "com.mysql.cj.jdbc.Driver");
        map.put(DBContextHolder.DATASOURCE_URL, "jdbc:mysql://localhost:3306/test2?useUnicode=true&" +
                "characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
        map.put(DBContextHolder.DATASOURCE_USERNAME, "root");
        map.put(DBContextHolder.DATASOURCE_PASSWORD, "root");
        DBContextHolder.setDBType(request, map);
        return ResponseEntity.ok(iSysConfigService.list());
    }

    @GetMapping("list2")
    public ResponseEntity<?> list2(){
        return ResponseEntity.ok(iSysConfigService.list());
    }


    @GetMapping("login")
    public ResponseEntity<?> login(HttpServletRequest request, AccountBook accountBook){
        String account = accountBook.getAccount();
        String password = accountBook.getPassword();
        AccountBook accountBook1 = iAccountBookService.selectByAccountPassword(account, password);
        if (accountBook1 == null){
            return ResponseEntity.ok("用户密码错误");
        }

        changeDB(request, accountBook1.getDbName(), accountBook1.getDbName(), accountBook1.getDbUserName(), accountBook1.getDbPassword());
        return ResponseEntity.ok("切换数据库为："+accountBook1.getDbName());
    }

    @GetMapping("clearn")
    public ResponseEntity<?> clearn(HttpServletRequest request){
        request.getSession().removeAttribute("contextHolder");
        return ResponseEntity.ok("ok");
    }

    private void changeDB(HttpServletRequest request, String dbKey, String dbName, String userName, String password){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(DBContextHolder.DATASOURCE_KEY, dbKey);
        map.put(DBContextHolder.DATASOURCE_DRIVER, "com.mysql.cj.jdbc.Driver");
        map.put(DBContextHolder.DATASOURCE_URL, "jdbc:mysql://localhost:3306/"+dbName+"?useUnicode=true&" +
                "characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
        map.put(DBContextHolder.DATASOURCE_USERNAME, userName);
        map.put(DBContextHolder.DATASOURCE_PASSWORD, password);
        DBContextHolder.setDBType(request, map);
    }
}
